// pages/report/report.ts
import api from '../../utils/api'
import Utils from '../../utils/utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visible: false as boolean,
    range: '全部' as string,//日期范围
    startTime: "" as string,
    endTime: "" as string,
    page: 1 as number,
    pageSize: 10 as number,
    totalCount: 0 as number,
    reportList: [] as ReportModel[],
    phone: '' as string,//查询他人带过来的手机号
  },
  // 日期范围
  getRange(e: any): void {
    // 选择日期后清空
    this.setData({
      page: 1,
      reportList: []
    })
    if (e.detail.data == "") {
      this.setData({
        range: "全部",
        startTime: "",
        endTime: ""
      })
    } else {
      this.setData({
        range: e.detail.data,
        startTime: e.detail.data.split("~")[0].trim(),
        endTime: e.detail.data.split("~")[1].trim(),
      })
    }
    this.close();
    this.queryReport()
  },
  // 打开弹窗
  show(): void {
    this.setData({
      visible: true
    })
  },
  // 关闭弹窗
  close(): void {
    this.setData({
      visible: false
    })
  },
  // 获取他人检测报告
  async queryReport(): Promise<void> {
    const { page, pageSize, startTime, endTime, reportList, phone } = this.data
    const params: ReportQueryReportParam = {
      page,
      pageSize,
      startTime,
      endTime,
      phone
    }
    const res = await api.report.queryReport(params)
    if (res.data.code == '000') {
      if (res.data.data && res.data.data.list) {
        res.data.data.list.forEach(item => {
          item.time = Utils.timestampToDateTime(item.time as number)
          item.sampleTime = item.time.replace(/[-\s:]/g, '').substring(0, 8);
          item.gender = item.gender == "FEMALE" ? "女" : "男"
          if (item.phone) {
            item.phone = item.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
          }
        })
        this.setData({
          totalCount: res.data.data.totalCount,
          reportList: [...reportList, ...res.data.data.list],
          page: page + 1
        })
      }
    } else {
      wx.showToast({
        title: res.data.message as string,
        icon: "none"
      })
    }
  },
  // 获取URL地址
  async getReportPDFUrl(e: any): Promise<void> {
    wx.showLoading({
      title: "加载中"
    })
    const url = e.currentTarget.dataset.url
    wx.downloadFile({
      url,
      success: function (res) {
        console.log(res)
        var filePath = res.tempFilePath;
        wx.openDocument({
          filePath: filePath,
          showMenu: true,
          fileType: 'pdf',
          success: function (res) {
            console.log('打开文档成功');
          },
          fail: function (res) {
            console.log('打开文档失败');
          }
        });
      },
      fail: function (res) {
        console.log('下载文件失败');
      },
      complete: function () {
        wx.hideLoading()
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    this.setData({
      phone: options.phone
    })
    this.queryReport();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.reportList.length == this.data.totalCount) {
      return
    }
    this.queryReport();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})